
package view;

import control.Calculos;
import javax.swing.JOptionPane;

public class CirculoForm extends javax.swing.JInternalFrame {
    //Variavel usadada na singleton
    private static CirculoForm instance;
    /** Creates new form CirculoForm */
    public CirculoForm() {
        initComponents();
        resultadoLabel.setVisible(false);
    }
    
    //Metodo usado para o singleton
    public static CirculoForm getInstance(){
        if(instance == null){
            instance = new CirculoForm();
        }
        return instance;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        raioLabel = new javax.swing.JLabel();
        raioTextField = new javax.swing.JTextField();
        calcularButton = new javax.swing.JButton();
        limparButton = new javax.swing.JButton();
        resultadoLabel = new javax.swing.JLabel();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Área do circulo");

        raioLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        raioLabel.setText("Raio:");

        raioTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        calcularButton.setText("Calcular");
        calcularButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcularButtonActionPerformed(evt);
            }
        });

        limparButton.setText("Limpar");
        limparButton.setMaximumSize(new java.awt.Dimension(71, 23));
        limparButton.setMinimumSize(new java.awt.Dimension(71, 23));
        limparButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparButtonActionPerformed(evt);
            }
        });

        resultadoLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        resultadoLabel.setForeground(new java.awt.Color(51, 51, 255));
        resultadoLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        resultadoLabel.setText("A área do circulo é: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(resultadoLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(raioLabel)
                        .addGap(10, 10, 10)
                        .addComponent(raioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(calcularButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(limparButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(raioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(calcularButton)
                    .addComponent(limparButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(raioLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(resultadoLabel)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcularButtonActionPerformed
    if(!raioTextField.getText().equals("")){
        Calculos calculo = new Calculos();
        resultadoLabel.setText("");
        try {
            resultadoLabel.setText(calculo.calcularAreaCirculo(Double.parseDouble(raioTextField.getText())));
        } catch (Exception e) {
            System.out.println(e.getClass().getCanonicalName() + ": " + e.getMessage());//para uso interno em testes
            JOptionPane.showMessageDialog(this, "Entrada Inválida");
            raioTextField.grabFocus();
        }
        resultadoLabel.setVisible(true);
    }else{
        JOptionPane.showMessageDialog(this, "O campo é obrigátorio para efetuar o cálculo");
        raioTextField.grabFocus();
    }
}//GEN-LAST:event_calcularButtonActionPerformed

private void limparButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparButtonActionPerformed
    raioTextField.setText("");
    resultadoLabel.setVisible(false);
    raioTextField.grabFocus();
}//GEN-LAST:event_limparButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton calcularButton;
    private javax.swing.JButton limparButton;
    private javax.swing.JLabel raioLabel;
    private javax.swing.JTextField raioTextField;
    private javax.swing.JLabel resultadoLabel;
    // End of variables declaration//GEN-END:variables
}
